package com.webapi.core.model.persist;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.webapi.core.model.common.TicketStatus;
import com.webapi.core.model.dto.TicketDto;

@Entity
@Table(name = "tb_ticket")
public class Ticket extends Base implements Serializable {

    private static final long serialVersionUID = -5045825242847878160L;

    @Column(name = "no", length = 14, nullable = false)
    private String no = "T" + System.currentTimeMillis();
    
    @Enumerated(EnumType.STRING)
    @Column(name = "status", length = 15, nullable = false)
    private TicketStatus status;
    
    @Column(name = "price", nullable = false, columnDefinition = "double default 0")
    private Double price;
    
    @Column(name = "name", length = 20, nullable = false)
    private String name;
    
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "order_id", nullable = false)
    private Order order;

    public String getNo() {
        return no;
    }

    public TicketStatus getStatus() {
        return status;
    }

    public void setStatus(TicketStatus status) {
        this.status = status;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }
    
    public static List<Ticket> valueOf(Order order, List<TicketDto> ticketDtos) {
        List<Ticket> tickets = new ArrayList<Ticket>();
        for (TicketDto ticketDto : ticketDtos) {
            Ticket ticket = new Ticket();
            ticket.setName(ticketDto.getName());
            ticket.setPrice(ticketDto.getPrice());
            ticket.setStatus(TicketStatus.Unissue);
            ticket.setOrder(order);
            tickets.add(ticket);
        }
        return tickets;
    }

    @Override
    public String toString() {
        return "Ticket [no=" + no + ", status=" + status + ", price=" + price
                + ", name=" + name + ", getId()=" + getId()
                + ", getCreateTime()=" + getCreateTime() + "]";
    }

}
